<!DOCTYPE html>
<html>
<head>
    <title>管理面板</title>
    <script>
        async function loadTexts() {
            const response = await fetch('/api/texts');
            const texts = await response.json();
            
            const list = document.getElementById('text-list');
            list.innerHTML = texts.map(text => `
                <li>
                    ${text.text} (${new Date(text.createdAt).toLocaleString()})
                    <button onclick="deleteText(${text.id})">删除</button>
                </li>
            `).join('');
        }

        async function deleteText(id) {
            const response = await fetch(`/api/texts/${id}`, {
                method: 'DELETE'
            });
            
            if (response.ok) {
                loadTexts(); // 刷新列表
            }
        }

        async function logout() {
            const response = await fetch('/api/sessions', {
                method: 'DELETE'
            });
            if (response.ok) {
                window.location.href = '/admin';
            }
        }

        // 页面加载时获取数据
        window.onload = loadTexts;
    </script>
</head>
<body>
    <h1>文本列表</h1>
    <ul id="text-list"></ul>
    <div>
        <a href="/">返回首页</a>
        <button onclick="logout()" style="margin-left: 10px;">退出登录</button>
    </div>
</body>
</html>
